function [OUTPUT, LEN]=splitting(data,context,T)
% Splitting data into streams and computing number of bits for arithmetic
% coding without auxilary information
n=length(data); % length of data stream
bit=0;%number of bits
bit_e=0;
Mi=min(context); % minimum context value
Ma=max(context); %maximum context value
j=1; %number of substreams
D=zeros(1,round(2*n/T));%current substream of data
i=Mi;
L=0; %-length of the current substream of data 
% Length of any stream except the last not less than n/T
total=0; %length of the processed part of data
%data
%pause
 number_streams=0;
 
 if T==1
 number_streams=number_streams+1;
 LEN(number_streams)=n;
 OUTPUT(number_streams,1:n)=data;       
 else

while ((i<=Ma)&(n-total)>round(n/T)) %Ma
        counter=1;
        L=0;
        D=zeros(1,round(2*n/T));
                     while (L<=round(n/T))
                           I=find(context==i);
                           l=length(I);
                           D(1,counter:counter+l-1)=data(I);%substream, corresponding to one context value (joint neighboring context values)  
                           counter=counter+l;
                           i=i+1;
                           L=L+l;
                     end;
                 
number_streams=number_streams+1;                     
OUTPUT(number_streams,1:n)=zeros(1,n);
%L
%counter
OUTPUT(number_streams,1:L)=D(1,1:L);
LEN(number_streams)=L;
total=total+L;
end;
%total
%n
%pause
% The last stream
       counter=1;
       D=zeros(1,round(2*n/T));
     if (n~=total) 
                  for j=i:Ma
                      I=find(context==j);
                      l=length(I);
                      D(1,counter:counter+l-1)=data(I);
                      counter=counter+l;
                  end;
number_streams=number_streams+1;
OUTPUT(number_streams,1:n)=zeros(1,n);
OUTPUT(number_streams,1:counter-1)=D(1,1:counter-1);
LEN(number_streams)=counter-1;
    end;
 end;    
    
    

